package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.common.utils.date.DateUtil;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.dto.BackendHomeDTO;
import com.ala4.oxcafe.entity.ServiceOrderDetail;
import com.ala4.oxcafe.repository.mapper.ServiceOrderDetailMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * 系统附件存储实体
 *
 * @author PING
 * @date 2025-03-25
 */
@Repository
public class ServiceOrderDetailRepository extends ServiceImpl<ServiceOrderDetailMapper, ServiceOrderDetail> implements IService<ServiceOrderDetail> {

    private LambdaQueryWrapper<ServiceOrderDetail> queryParam(ServiceOrderDetail serviceOrderDetail) {

        LambdaQueryWrapper<ServiceOrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrderDetail.getServiceId()), ServiceOrderDetail::getServiceId, serviceOrderDetail.getServiceId());
        return queryWrapper;
    }

    public IPage<ServiceOrderDetail> selectServiceOrderDetailPage(PageDomain pageDomain, ServiceOrderDetail serviceOrderDetail) {

        return this.page(pageDomain.toPlusPage(), queryParam(serviceOrderDetail));
    }

    public List<ServiceOrderDetail> selectServiceOrderDetailList(ServiceOrderDetail serviceOrderDetail) {
        return this.list(queryParam(serviceOrderDetail));
    }

    public List<BackendHomeDTO.MonthProductDTO> monthProductCount() {
        return this.baseMapper.monthProductCount(DateUtil.firstDayOfMonth(LocalDate.now()), DateUtil.lastDayOfMonth(LocalDate.now()));
    }

    public List<BackendHomeDTO.SixMonthProduct> lastSixMonthProductCount() {
        return this.baseMapper.lastSixMonthProductCount(DateUtil.firstDayOfMonth(LocalDate.now()).plusMonths(-5), DateUtil.lastDayOfMonth(LocalDate.now()));
    }
}
